package com.arthur.shopping.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.arthur.common.domain.Tree;
import com.arthur.common.service.impl.BaseService;
import com.arthur.common.util.TreeUtils;
import com.arthur.shopping.dao.TypeMapper;
import com.arthur.shopping.domain.Type;
import com.arthur.shopping.service.TypeService;

@Service("typeService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TypeServiceImpl extends BaseService<Type> implements TypeService {

	@Autowired
	private TypeMapper typeMapper;
	
	
	@Override
	public List<Type> findAllTypes(Type type) {
		Example example = new Example(Type.class);
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(type.getTypeName())) {
			criteria.andCondition("type_name=", type.getTypeName());
		}
		example.setOrderByClause("id");
		return this.selectByExample(example);
	}


	@Override
	public Tree<Type> getTypeNameTree() {
		List<Tree<Type>> trees = new ArrayList<Tree<Type>>();
		Example example = new Example(Type.class);
		example.setOrderByClause("create_time");
		List<Type> types = this.typeMapper.selectByExample(example);
		for (Type type : types) {
			Tree<Type> tree = new Tree<Type>();
			tree.setId(type.getTypeName());
			tree.setUrl(type.getId().toString());
			tree.setIcon(type.getIcon());
			tree.setParentId(type.getParentId().toString());
			tree.setText(type.getTypeName());
			trees.add(tree);
		}
		Tree<Type> t = TreeUtils.build(trees);
		return t;
	}

	@Override
	public Tree<Type> getTypeTree() {
		List<Tree<Type>> trees = new ArrayList<Tree<Type>>();
		Example example = new Example(Type.class);
		example.setOrderByClause("create_time");
		List<Type> types = this.typeMapper.selectByExample(example);
		for (Type type : types) {
			Tree<Type> tree = new Tree<Type>();
			tree.setId(type.getId().toString());
			tree.setParentId(type.getParentId().toString());
			tree.setText(type.getTypeName());
			trees.add(tree);
		}
		Tree<Type> t = TreeUtils.build(trees);
		return t;
	}

	@Override
	public Type findByName(String typeName) {
		Example example = new Example(Type.class);
		example.createCriteria().andCondition("lower(type_name)=", typeName.toLowerCase());
		List<Type> list = this.typeMapper.selectByExample(example);
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void addType(Type type) {
		type.setCreateTime(new Date());
		if (type.getParentId() == null)
			type.setParentId(0l);
		this.save(type);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void deleteMeuns(String typeIds) {
		List<String> list = Arrays.asList(typeIds.split(","));
		this.batchDelete(list, "id", Type.class);
		this.typeMapper.changeToTop(list);
	}

	@Override
	public Type findById(Long typeId) {
		return this.selectByKey(typeId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void updateType(Type type) {
		type.setModifyTime(new Date());
		if (type.getParentId() == null)
			type.setParentId(0l);
		this.updateNotNull(type);
	}

}
